package com.evon.yardmanagement.export;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.evon.yardmanagement.model.Section;
import com.evon.yardmanagement.model.Yard;

/**
 * Default implementation of the exportable data.
 * 
 * @author vgaurav
 * 
 */
public class SectionExportableData implements ExportableData, ImportableData {

	private static final int COLUMN_CAPACITY = 9;

	private static final int COLUMN_UPDATED_USER = 8;

	private static final int COLUMN_CREATED_USER = 7;

	private static final int COLUMN_UPDATED_TIME = 6;

	private static final int COLUMN_CREATED_TIME = 5;

	private static final int COLUMN_IS_ACTIVE = 4;

	private static final int COLUMN_IS_PERMANENT = 3;

	private static final int COLUMN_SECTION_NAME = 2;

	private static final int COLUMN_YARD_ID = 1;

	private static final int COLUMN_SECTION_ID = 0;

	private static final String CAPACITY = "CAPACITY";

	private static final String HSC_ID = "HSC_ID";

	private static final String HSU_TS = "HSU_TS";

	private static final String HSC_TS = "HSC_TS";

	private static final String ACTIVE = "ACTIVE";

	private static final String PERMANENT = "PERMANENT";

	private static final String SECTION_NAME = "SECTION_NAME";

	private static final String YARD_ID = "YARD_ID";

	private static final String SECTION_ID = "SECTION_ID";

	private static final String SECTIONS = "SECTIONS";

	private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");;

	// Data to be exported
	private List<Section> sectionDataList;

	private File fileToImport;

	public SectionExportableData(List<Section> listSection) {
		this.sectionDataList = listSection;
	}

	public int getColumnCount() {
		return 10;
	}

	/**
	 * Return the wrapped data.
	 * 
	 * @return
	 */
	public List<Section> getData() {
		return sectionDataList;
	}

	public int getRowCount() {
		if (sectionDataList == null || sectionDataList.isEmpty()) {
			return 0;
		}
		return sectionDataList.size();
	}

	public List<String> getColumnHeader() {
		// TODO Auto-generated method stub
		return Arrays.asList(SECTION_ID, YARD_ID, SECTION_NAME, PERMANENT,
				ACTIVE, HSC_TS, HSU_TS, HSC_ID, HSC_ID, CAPACITY);
	}

	/**
	 * 
	 */
	public String getValueAt(int row, int column) {
		if (sectionDataList == null || sectionDataList.isEmpty()) {
			throw new IllegalStateException(
					"The SectionExportableData has no model set, please set the list of model first before calling this method.");
		}
		if (row >= sectionDataList.size() || column > getColumnCount()) {
			throw new IllegalArgumentException(
					"The row and column range is out of bound. Chcek the row and column size being passed to this method.");
		}

		Section section = sectionDataList.get(row);

		if (section == null) {
			throw new IllegalStateException(
					"Section iside sectionDataList is null.");
		}

		switch (column) {
		case COLUMN_SECTION_ID:
			return String.valueOf(section.getId());
		case COLUMN_YARD_ID:
			return String.valueOf(section.getYard().getId());
		case COLUMN_SECTION_NAME:
			return section.getName();
		case COLUMN_IS_PERMANENT:
			return String.valueOf(section.getIsPermanent());
		case COLUMN_IS_ACTIVE:
			return String.valueOf(section.getIsActive());
		case COLUMN_CREATED_TIME:

			return formatter.format(section.getCreatedTime());
		case COLUMN_UPDATED_TIME:
			return formatter.format(section.getLastUpdatedTime());
		case COLUMN_CREATED_USER:
			return section.getCreatedUserName();
		case COLUMN_UPDATED_USER:
			return section.getLastUpdatedUserName();
		case COLUMN_CAPACITY:
			return String.valueOf(section.getCapacity());
		default:
			return "";
		}

	}

	public String getDataName() {
		// TODO Auto-generated method stub
		return SECTIONS;
	}

	public void setValueAt(int row, int column, Object value) {
		if (sectionDataList == null) {
			sectionDataList = new ArrayList<Section>();
		}
		if (column > getColumnCount()) {
			throw new IllegalArgumentException(
					"Column size can not be greater than " + getColumnCount());
		}

		if (sectionDataList.size() - 1 < row) {
			sectionDataList.add(new Section());
		}
		Section section = sectionDataList.get(row);

		switch (column) {
		case COLUMN_SECTION_ID:
			// for new Section

			section.setId((Integer) value);

			break;
		case COLUMN_YARD_ID:
			Yard yard = new Yard();
			yard.setId((Integer) value);
			section.setYard(yard);
			break;
		case COLUMN_SECTION_NAME:
			section.setName((String) value);
			break;
		case COLUMN_IS_PERMANENT:
			section.setIsPermanent((Integer) value);
			break;

		case COLUMN_IS_ACTIVE:
			section.setIsActive((Integer) value);
			break;
		case COLUMN_CREATED_TIME:
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d;
			try {
				d = (Date) formatter.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d = new java.util.Date();
			}
			section.setCreatedDate(d);
			break;
		case COLUMN_UPDATED_TIME:
			DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d1;
			try {
				d1 = (Date) formatter1.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d1 = new java.util.Date();
			}
			section.setUpdatedDate(d1);
			break;

		case COLUMN_CREATED_USER:
			section.setCreatedBy((String) value);
			break;

		case COLUMN_UPDATED_USER:
			section.setUpdatedBy((String) value);
			break;

		case COLUMN_CAPACITY:
			section.setCapacity((Integer) value);
			break;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ column);
		}
	}

	public File getFileToImport() {
		// TODO Auto-generated method stub
		return fileToImport;
	}

	public void setFileToImport(File fileToImport) {
		this.fileToImport = fileToImport;

	}

	public ExportCellType getColumnClass(int columnNumber) {
		switch (columnNumber) {
		case COLUMN_SECTION_ID:
			// for new Section

			return ExportCellType.NUMBER;

		case COLUMN_YARD_ID:
			return ExportCellType.NUMBER;

		case COLUMN_SECTION_NAME:
			return ExportCellType.STRING;
		case COLUMN_IS_PERMANENT:
			return ExportCellType.NUMBER;

		case COLUMN_IS_ACTIVE:
			return ExportCellType.NUMBER;

		case COLUMN_CREATED_TIME:
			return ExportCellType.DATE;
		case COLUMN_UPDATED_TIME:
			return ExportCellType.DATE;

		case COLUMN_CREATED_USER:
			return ExportCellType.STRING;

		case COLUMN_UPDATED_USER:
			return ExportCellType.STRING;

		case COLUMN_CAPACITY:
			return ExportCellType.NUMBER;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ columnNumber);
		}

	}

}
